package edu.scau.mis.pos.controller;


import edu.scau.mis.pos.entity.PosOrder;
import edu.scau.mis.pos.service.IOrderItemService;
import edu.scau.mis.pos.service.IOrderService;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (PosOrder)表控制层
 *
 * @author makejava
 * @since 2021-11-04 22:40:02
 */
@RestController
@RequestMapping("pos_order")
@ResponseBody
public class OrderController {
    /**
     * 服务对象
     */
    @Resource
    private IOrderService iOrderService;


    @Autowired
    private IOrderItemService orderItemService;
    /**
     * 分页查询
     *
     * @param posOrder 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public List<PosOrder> queryAll(PosOrder posOrder) {
        return iOrderService.queryAll(posOrder);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param orderId 主键
     * @return 单条数据
     */
    @GetMapping("{orderId}")
    public PosOrder queryById(@PathVariable("orderId") Integer orderId) {
        return this.iOrderService.queryById(orderId);
    }

    /**
     * 新增数据
     *
     * @param posOrder 实体S
     * @return 新增结果
     */
    @PostMapping("/addOrder")
    public PosOrder addOrder(PosOrder posOrder) {
        return this.iOrderService.insertNext(posOrder);
    }

    @GetMapping("/queryLastOrderNo")
    public String queryLastOrderNo(PosOrder posOrder){
        String orderNextNo=  this.iOrderService.queryLastOrderNo(posOrder);
        this.orderItemService.getOrderAndBuyInformationByOrderNo(orderNextNo);
        System.out.println(orderNextNo);
        return this.iOrderService.queryLastOrderNo(posOrder);
    }

    /**
     * 编辑数据
     *
     * @param posOrder 实体
     * @return 编辑结果
     */
    @PutMapping
    public String edit(@RequestBody PosOrder posOrder) {
        this.iOrderService.update(posOrder);
        return "ok";
    }


    /**
     * 删除数据
     *
     * @param orderId 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete/{orderId}")
    public boolean deleteById(@PathVariable Integer orderId) {
        return this.iOrderService.deleteById(orderId);
    }


}

